<?php

namespace App\Modules\Apicrzs\Entities;

use App\Models\Model;
use Carbon\Carbon;

class UserScoreModel extends Model
{
    protected $table='user_scores';

    protected $primaryKey='id';
    protected $is_delete=0;

    const  BACK = -1;
    const  GAIN = 0;
    const  TAKE = 1;


    /**
     * 每天要统计
     * @param $params
     */
    public function getUserScoreDays($params){
        $query = $this->query()->selectRaw("sum(score) AS value, from_unixtime(gift_time,'%Y-%m-%d') as times");
        $query = $query->where('user_id', $params['user_id']);
        $query = $query->where('flag', 0);
        $query = $query->where('is_delete', 0);
        $query = $query->whereBetween($this->table . '.gift_time', [$params['start_time'], $params['end_time']]);
        $query = $query->groupBy( 'times' );
        return  $query->get()->toArray();
    }
    /**
     * 累计
     * @param $params
     * @return mixed[]
     */
    public function getUserAccumulateScore($params){

        $start_time = Carbon::parse($params['month'])->startOfMonth()->toDateString();
        $end_time = Carbon::parse($params['month'])->endOfMonth()->toDateString();
        $start_time = strtotime($start_time . ' 00:00:00');
        $end_time = strtotime($end_time . ' 23:59:59');

        $query = $this->query()->selectRaw("water_id,sum(score) AS value");
        $query = $query->where('user_id', $params['user_id']);
        $query = $query->where('flag', 0);
        $query = $query->where('is_delete', 0);
        $query = $query->whereBetween($this->table . '.gift_time', [$start_time, $end_time]);
        $query = $query->groupBy( 'water_id' );
        return $query->get()->toArray();
    }

    /**
     * 每天要统计
     * @param $params
     * @return mixed[]
     */
    public function getUserScoreLastDays($params){
        //昨日积分
        $day_time = date("Y-m-d", strtotime("-1 day"));
        $start_time = strtotime($day_time . ' 00:00:00');
        $end_time = strtotime($day_time . ' 23:59:59');
        $query = $this->query()->selectRaw("sum(score) AS value,water_id");
        $query = $query->where('user_id', $params['user_id']);
        $query = $query->where('is_delete', 0);
        $query = $query->where('flag', 0);
        $query = $query->whereBetween('gift_time', [$start_time, $end_time]);
        $query = $query->groupBy( 'water_id' );

        $scoreLastDays = $query->get()->toArray();

        $newArray=[];
        foreach ($scoreLastDays as $item) {
            $waterId = $item['water_id'];
            $value = $item['value'];
            $newArray[$waterId] = $value;
        }
        return  $newArray;
    }
    /**
     * 水机 积分 统计
     * @param $params
     * @return mixed[]
     */
    public function getUserScoreSumCount($params){

        $day_time = date("Y-m-d", strtotime("-1 day"));
        $end_time = strtotime($day_time . ' 23:59:59');

        $query = $this->query()->selectRaw("sum(score) AS value,water_id");
        $query = $query->where('user_id', $params['user_id']);
        $query = $query->where('flag', 0);
        $query = $query->where('is_delete', 0);
        $query = $query->where('gift_time', '<',$end_time);
        $query = $query->groupBy( 'water_id' );
        $scoreLastDays = $query->get()->toArray();

        $newArray=[];
        foreach ($scoreLastDays as $item) {
            $waterId = $item['water_id'];
            $value = $item['value'];
            $newArray[$waterId] = $value;
        }
        return  $newArray;
    }
}
